package com.wangc.algorithm.binarysearch;


/**
 * @author wangC
 * @create 2020-07-13 下午10:33
 * @Description
 **/
public class BSNearLeft {

    //在arr上，找满足>=value的最左位置,arr有序
    public static int nearestIndex(int[] arr, int value) {
        int L = 0 ;
        int R = arr.length - 1;
        int index = -1;
        while (L <= R) {
            int mid = L + ((R - L) >> 1);
            if (arr[mid] >= value) {
                index = mid;
                R = mid - 1;
            } else {
                L = mid + 1;
            }
        }
        return index;
    }

    int bsNearLeftTest(int[] arr, int num) {
        int L = 0 ;
        int R = arr.length - 1;
        int index = -1;
        while (L <= R) {
            int mid = L + (R - L) >> 2;
            if (arr[mid] > num) {
                index = mid;
                R = mid - 1;
            } else {
                L = mid + 1;
            }
        }
        return index;
    }


    public int nearestIndexTest1(int[] arr, int value) {

        if (arr == null || arr.length == 0) {
            return -1;
        }

        int L = 0;
        int R = arr.length - 1;
        int index = -1;
        while (L <= R) {
            int mid = L + ((R - L) >> 1);
            if (arr[mid] >= value) {
                index = mid;
                R = R - 1;
            } else {
                L = L + 1;
            }
        }
        return index;
    }

    public int nearestIndexTest22(int[] arr, int value) {
        if (arr == null || arr.length == 0) {
            return -1;
        }
        int L = 0;
        int R = arr.length - 1;
        int index = -1;
        while (L < R) {
            int mid = L + (R - L) >> 1;
            if (arr[mid] >= value) {
                index = mid;
                R = mid - 1;
            } else {
                L = mid + 1;
            }
        }
        return index;
    }

    public int bsNearLeftTest33(int[] arr, int value) {
        if (arr == null || arr.length == 0) {
            return -1;
        }
        int L = 0;
        int R = arr.length - 1;
        int index = -1;
        int mid = 0;
        while (L < R) {
            mid = L + (R - L) >> 1;
            if (arr[mid] > value) {
                index = mid;
                R = mid - 1;
            } else {
                L = mid + 1;
            }
        }
        return index;
    }

}
